package com.htjc.integral.account.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.htjc.integral.account.annotation.InvokeLog;
import com.htjc.integral.account.service.IAccountService;
import com.htjc.integral.account.service.ICenterService;
import com.htjc.integral.account.service.INewAccountService;
import com.htjc.integral.account.util.JsonUtil;
import com.htjc.integral.account.util.ResultModel;
import com.htjc.integral.account.util.enums.AccountReturn;
import com.htjc.integral.common.util.StringUtil;
import com.htjc.uep.center.commons.utils.SignUtil;
import com.htjc.uep.center.model.enums.ErrorStatus;
import com.htjc.uep.center.model.service.request.DeductionPointsRequest;

import lombok.extern.slf4j.Slf4j;

/**
 * 新账户体系  controller层
 * 
 *
 * @author hlc
 */
@Slf4j(topic = "jfLog")
@RestController
@RequestMapping(value = "/account")
public class NewAccountNoSignController {
	
	@Autowired
	private IAccountService accountService;

	@Autowired
	private INewAccountService accountServiceImpl;
	
	@Autowired
	private ICenterService centerService;

	/**
	 * 积分划转(暂不支持浙江山东福建湖北)
	 * @param params
	 * @return
	 */
	@InvokeLog(value="新积分划转",params={"ACCOUNT_NAME","BUSINESS_ID","ACCOUNT_ID","CHANNEL_NO"
			,"POWER_USER_LIST","DEAL_TIME","ACCT_DATE"})
	@RequestMapping(value = {"/changeIntegral"}, produces = {"text/json;charset=UTF-8;", "application/json;"})
	public String changeIntegral(@RequestBody String params){
		JSONObject resultJson=new JSONObject();
		try {
			resultJson= accountServiceImpl.changeIntegral(JSON.parseObject(params),"01");
		} catch (Exception e) {
			log.error("【新积分划转】异常",e);
			return ResultModel.ReturnErrorJson().toString();
		}
		return resultJson.toString();
	}

	/**
	 * 缴费积分划转(暂不支持浙江山东福建湖北)
	 * @param params
	 * @return
	 */
	@InvokeLog(value="新缴费积分划转",params={"ACCOUNT_NAME","BUSINESS_ID","ACCOUNT_ID","CHANNEL_NO"
			,"POWER_USER_LIST","DEAL_TIME","ACCT_DATE"})
	@RequestMapping(value = {"/payChangeIntegral"}, produces = {"text/json;charset=UTF-8;", "application/json;"})
	public String payChangeIntegral(@RequestBody String params){
		JSONObject resultJson=new JSONObject();
		try {
			//01：积分划转 02：缴费积分划转
			resultJson=accountServiceImpl.changeIntegral(JSON.parseObject(params),"02");
		} catch (Exception e) {
			log.error("【新缴费积分划转】异常",e);
			return ResultModel.ReturnErrorJson().toString();
		}
		return resultJson.toString();
	}

	/**
	 * 积分余额查询
	 * @param params
	 * @return
	 */
	@InvokeLog(value="新积分余额查询",params={"ACCOUNT_ID","CHANNEL_NO","ACCOUNT_NAME","BUSINESS_ID"})
	@RequestMapping(value = {"/queryIntegral"}, produces = {"text/json;charset=UTF-8;", "application/json;"})
	public String queryIntegral(@RequestBody String params){
		JSONObject resultJson=new JSONObject();
		try {
			resultJson= accountServiceImpl.queryIntegral(JSON.parseObject(params));
		} catch (Exception e) {
			log.error("【新积分余额查询】异常",e);
			return ResultModel.ReturnErrorJson().toString();
		}
		return resultJson.toString();
	}

	/**
	 * 积分明细查询
	 * @param params
	 * @return
	 */
	@InvokeLog(value="新积分明细查询",params={"ACCOUNT_ID","CHANNEL_NO","PAGE_INDEX"})
	@RequestMapping(value = {"/queryIntegralDetail"}, produces = {"text/json;charset=UTF-8;", "application/json;"})
	public String queryIntegralDetail(@RequestBody String params) {
		return accountServiceImpl.queryIntegralDetail(JSON.parseObject(params)).toString();
	}

	/**
	 * 积分余额增加
	 * @param params
	 * @return
	 */
	@InvokeLog(value="新积分余额增加",params={"ACCOUNT_ID","CHANNEL_NO","ACCOUNT_NAME","BUSINESS_ID"
			,"BUSS_TYPE","OPERATION_TYPE","BUSS_DISCRIBLE","INTEGRAL","DEAL_TIME","CREATE_DATE","ACCT_DATE"})
	@RequestMapping(value = {"/addIntegral"}, produces = {"text/json;charset=UTF-8;", "application/json;"})
	public String addIntegral(@RequestBody String params){
		JSONObject resultJson=new JSONObject();
		try {
			resultJson= accountServiceImpl.addIntegral(JSON.parseObject(params));
		} catch (Exception e) {
			log.error("【新积分余额增加】异常",e);
			return ResultModel.ReturnErrorJson().toString();
		}
		return resultJson.toString();
	}

	/**
	 * 积分余额扣减
	 * @param params
	 * @return
	 */
	@InvokeLog(value="新积分余额扣减",params={"ACCOUNT_ID","CHANNEL_NO","ACCOUNT_NAME","OPERATION_TYPE"
			,"BUSS_TYPE","BUSS_DISCRIBLE","BUSINESS_ID","INTEGRAL","DEAL_TIME","ACCT_DATE"})
	@RequestMapping(value = {"/deductionIntegral"}, produces = {"application/json;"})
	public String deductionIntegral(@RequestBody String params){
		JSONObject resultJson=new JSONObject();
		try {
			resultJson= accountServiceImpl.deductionIntegral(JSON.parseObject(params),true);
		} catch (Exception e) {
			log.error("【新积分余额扣减】异常",e);
			return ResultModel.ReturnErrorJson().toString();
		}
		return resultJson.toString();
	}
	
	/**
	 * 账户户号积分查询
	 * @param params
	 * @return
	 */
	@InvokeLog(value="账户户号积分查询",params={"ACCOUNT_ID","CHANNEL_NO","BUSINESS_ID","TOKEN"})
	@RequestMapping(value = {"/queryConsNoIntegral"}, produces = {"application/json;"})
	public String queryConsNoIntegral(@RequestBody String params){
		JSONObject resultJson=new JSONObject();
		try {
			resultJson= accountServiceImpl.queryConsNoIntegral(JSON.parseObject(params));
		} catch (Exception e) {
			log.error("【账户户号积分查询】异常",e);
			return ResultModel.ReturnErrorJson().toString();
		}
		return resultJson.toString();
	}
	
	/**
	 * 户号明细查询
	 * @param params
	 * @return
	 */
	@InvokeLog(value="户号明细查询",params={"ACCOUNT_ID","CHANNEL_NO","CONS_NO","PAGE_INDEX"})
	@RequestMapping(value = {"/queryConsNoIntegralDetail"}, produces = {"application/json;"})
	public String queryConsNoIntegralDetail(@RequestBody String params){
		JSONObject resultJson=new JSONObject();
		try {
			resultJson= accountServiceImpl.queryConsNoIntegralDetail(JSON.parseObject(params));
		} catch (Exception e) {
			log.error("【户号明细查询】异常",e);
			return ResultModel.ReturnErrorJson().toString();
		}
		return resultJson.toString();
	}
	
	/**
	 * 账户过期积分查询
	 * @param params
	 * @return
	 */
	@InvokeLog(value="账户过期积分查询",params={"ACCOUNT_ID","CHANNEL_NO"})
	@RequestMapping(value = {"/queryStaleIntegral"}, produces = {"application/json;"})
	public String queryStaleIntegral(@RequestBody String params){
		JSONObject resultJson=new JSONObject();
		try {
			resultJson= accountServiceImpl.queryStaleIntegral(JSON.parseObject(params));
		} catch (Exception e) {
			log.error("【账户过期积分查询】异常",e);
			return ResultModel.ReturnErrorJson().toString();
		}
		return resultJson.toString();
	}
	
	/**
	 * 老系统积分划转专用插入数据库接口
	 * @param params
	 * @return
	 */
	@InvokeLog(value="老系统积分划转专用插入数据库接口",params={"ACCOUNT_ID","ACCOUNT_NAME","POWER_USER_NO","PROVINCE_CODE","ORG_NO","POWERID","CHANNEL_NO","BUSINESS_ID","BALANCE"})
	@RequestMapping(value = {"/changeInsertDb"}, produces = {"application/json;"})
	public String changeInsertDb(@RequestBody String params){
		JSONObject resultJson=new JSONObject();
		try {
			resultJson= accountServiceImpl.changeInsertDb(JSON.parseObject(params));
		} catch (Exception e) {
			log.error("【户号明细查询】异常",e);
			return ResultModel.ReturnErrorJson().toString();
		}
		return resultJson.toString();
	}
	
	/**
	 * 新老系统都有积分数据的账户处理
	 * @param 
	 * @return
	 */
	@InvokeLog(value="新老系统都有积分数据的账户处理")
	@RequestMapping(value = {"/selectAccountDb"}, produces = {"application/json;"})
	public String selectAccountDb(@RequestBody String params){
		JSONObject resultJson=new JSONObject();
		try {
			resultJson=accountServiceImpl.selectAccountDbList(JSON.parseObject(params));
		} catch (Exception e) {
			log.error("【查询新系统所有账户信息接口】异常",e);
			return ResultModel.ReturnErrorJson().toString();
		}
		return resultJson.toString();
	}
	
	
	/**
	 * 交费用户积分划转（浙江）
	 * @param json
	 * @return json
	 */
	@InvokeLog(value="交费用户积分划转",params={"ACCOUNT_MAIN_ID","LOGIN_ID","UN_ID","LIST","POWERUSERNOLIST"})
	@RequestMapping(value = {"/payChangeIntegralZj"}, produces = {"text/json;charset=UTF-8;", "application/json;"})
	public String payChangeIntegralZj(@RequestBody String params){
		//1、校验参数
		JSONObject json = JSON.parseObject(params);
		json.put("IP","127.0.0.1");
//		headerMap.put("CHANNEL_RESOURCE",json.getString("CHANNEL_NO"));//渠道编码
		json.put("CHANNEL_RESOURCE","5");//渠道编码
		json.put("USER_NAME","网上国网");//
		//循环域
		JSONArray listJson = JSON.parseArray(json.getString("LIST"));
		for(int i=0;i<listJson.size();i++) {
			// 遍历array数组,把每一个对象转成 json 对象
			JSONObject jsonObject = listJson.getJSONObject(i);
			//校验网省编号
			if (StringUtil.isNull(jsonObject.getString("PROVINCE_CODE"))) {
				return JSONObject.parseObject(JsonUtil.bean2json(ResultModel.AccountReturnError(AccountReturn.PROVINCE_CODE_ERROR_NULL))).toString();
			}
			//校验供电单位编码
			if (StringUtil.isNull(jsonObject.getString("ORG_NO"))) {
				return JSONObject.parseObject(JsonUtil.bean2json(ResultModel.AccountReturnError(AccountReturn.ORG_NO_ERROR_NULL))).toString();
			}
			//校验户号ID
			if (StringUtil.isNull(jsonObject.getString("POWER_USER_NO"))) {
				return JSONObject.parseObject(JsonUtil.bean2json(ResultModel.AccountReturnError(AccountReturn.POWER_USER_NO_ERROR_NULL))).toString();
			}
			//校验交费金额
			if (StringUtil.isNull(jsonObject.getString("PAY_NUM"))) {
				return JSONObject.parseObject(JsonUtil.bean2json(ResultModel.AccountReturnError(AccountReturn.PAY_NUM_ERROR_NULL))).toString();
			}
			//校验交费时间
			if (StringUtil.isNull(jsonObject.getString("CREATE_TIME"))) {
				return JSONObject.parseObject(JsonUtil.bean2json(ResultModel.AccountReturnError(AccountReturn.CREATE_TIME_ERROR_NULL))).toString();
			}
		}
		return accountService.payChangeIntegralByDxp(json).toString();
	}
	
	/**
	 * 用户点击积分中心积分划转（浙江）
	 * @param json( PROVINCE_CODE	网省编号
	 *				ORG_NO	供电单位编码
	 *				BUSINESS_TYPE	业务类型
	 *				BUSINESS_ID	业务ID
	 *				LOGIN_ACCOUNT_ID	登录账户ID)
	 * @return json
	 */
//	@InvokeLog(value="用户点击积分中心积分划转",params={"PROVINCE_CODE","ORG_NO","BUSINESS_TYPE","BUSINESS_ID","LOGIN_ACCOUNT_ID"})
	@InvokeLog(value="用户点击积分中心积分划转",params={"LOGIN_ACCOUNT_ID"})
	@RequestMapping(value = {"/changeIntegralZj"}, produces = {"text/json;charset=UTF-8;", "application/json;"})
	public String changeIntegralZj(@RequestBody String params){
		JSONObject json = JSON.parseObject(params);
		json.put("IP","127.0.0.1");
//		headerMap.put("CHANNEL_RESOURCE",json.getString("CHANNEL_NO"));//渠道编码
		json.put("CHANNEL_RESOURCE","5");//渠道编码
		json.put("USER_NAME","网上国网");//
		return accountService.changeIntegralByDxp(json).toString();
	}
	
	/**
	 * 河南积分抽奖用，无验签
	 * @param str
	 * @return
	 */
	@RequestMapping(value = {"business/dedPointApplyNoSign"},produces = {"text/json;charset=UTF-8;", "application/json;"})
	public String dedPointApplyNoSign(@RequestBody String str) {
		try {
			log.info("积分中心-积分池扣减业务申请接口-入参={}", str);
			DeductionPointsRequest businessApplyRequest = com.htjc.uep.center.commons.utils.JsonUtil.json2Bean(str, DeductionPointsRequest.class);
			
			String dealIdSign = businessApplyRequest.getDealId();
			String mechanismCodeSign = businessApplyRequest.getMechanismCode();
			String accountCodeSign = businessApplyRequest.getAccountCode();
			String acctDateSign = businessApplyRequest.getAcctDate();
			String userIdSign = businessApplyRequest.getUserId();
			String phoneSign = businessApplyRequest.getPhone();
			String scoreSign = String.valueOf(businessApplyRequest.getScore());
			String bpTypeSign = businessApplyRequest.getBpType();
			String sendTimeSign = businessApplyRequest.getSendTime();
			String channelSign = businessApplyRequest.getChannel();
			
			//判断入参是否为空
			if (com.htjc.uep.center.commons.utils.StringUtil.isEmpty(dealIdSign, mechanismCodeSign, accountCodeSign, acctDateSign, userIdSign, phoneSign, scoreSign, bpTypeSign, sendTimeSign, channelSign)) {
				log.info("====>>>>积分扣减业务<<<<====积分扣减业务失败，入参存在空值-入参={}", str);
				return com.htjc.uep.center.commons.utils.JsonUtil.getError(ErrorStatus.ERROR_PARAM);
			}
			
//			String signParam = dealIdSign + accountCodeSign + mechanismCodeSign + acctDateSign + userIdSign + phoneSign
//					 + scoreSign + bpTypeSign + sendTimeSign + channelSign;
			//验签
			SignUtil signUtil = SignUtil.getInstance();
			
//			if (!signUtil.verifySign(businessApplyRequest.getSign(), signParam)) {
//				log.info("====>>>>积分扣减业务<<<<====验签失败");
//				return JsonUtil.getError(ErrorStatus.ERROR_SIGN);
//			}
			//字段解密-0 加密 1解密
			businessApplyRequest = signUtil.getDEBean(businessApplyRequest, "1");
			
			log.info("积分中心-积分池扣减业务申请接口-积分池扣减");
			String response = centerService.deductionPointsApply(businessApplyRequest);
			return response;
		} catch (Exception e) {
			log.error("", e);
			return com.htjc.uep.center.commons.utils.JsonUtil.getError(ErrorStatus.ERROR_UNKNOWN);
		}	
	}

}
